.PHONY : dev test run

all:
	@echo "This make-file supports no default target."
	@echo "Please choose one of the following:"
	@echo "  image            build docker image from Dockerfile"
	@echo "  devel-image      build docker image for development"
	@echo "  dev              develop sources with devel-image in console"
	@echo "  test             debug the built image in console"
	@echo "  run              run the built image as a service"
	@echo ""

######################################################################
# Clear old image
######################################################################
define clear_image
	@echo ""
	@echo "🐳  Clearing image '$1'..."
	-@docker ps -a|grep -e $1|awk -F ' ' '{print $$1}'|xargs -r docker rm -v
	-@docker image rm $1
endef

define clear_useless_image
	-@docker ps -a | grep -e Exited | awk -F ' ' '{print $$1}' | xargs -r docker rm -v
	-@docker images | grep -e none | awk -F ' ' '{print $$3}' | xargs -r docker image rm
endef

define exit
	-@docker stop $1
	-@docker rm -v $1
endef

cu:
#	-@docker system prune -f
	$(call clear_useless_image)


######################################################################
# Build new image
######################################################################
create_docker_network:
	-@docker network create -o "com.docker.network.bridge.name"="freeipa_net" --subnet 192.168.10.0/24 freeipa_net

exit-dev:
	$(call exit, "web-dev")
	$(call exit, "nscan-dev")

build: create_docker_network exit-dev
		docker-compose build nscan-dev
		docker-compose build web-dev

dev: build cu
	-@sudo rm -rf ./data/web-dev
	-@docker-compose run -p 5000:5000 --name web-dev web-dev

db:
	-@docker-compose stop redis phpmyadmin mysql
	-@docker-compose up -d redis phpmyadmin

enter:
	-@docker exec -it web-dev bash
